package com.example.ice.u17.fragment;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.example.ice.u17.BR;
import com.example.ice.u17.R;
import com.example.ice.u17.adapter.Bou_RecyclerAdapter;
import com.example.ice.u17.entries.BoutiqueBean;
import com.example.ice.u17.entry.BouItemSecond;
import com.example.ice.u17.entry.Bou_ComicList_Item;
import com.example.ice.u17.entry.Bou_ReturnData;
import com.example.ice.u17.util.ClassType;
import com.example.ice.u17.util.NetUtils;
import com.example.ice.u17.util.PullToRefresh;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BoutiqueFragment extends Fragment implements Callback<BoutiqueBean> {


    private ViewDataBinding binding;
    private PullToRefresh ptr;
    private RecyclerView recycle;
    private Bou_RecyclerAdapter bou_recyclerAdapter;
    private ProgressBar progressbar;

    public BoutiqueFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         binding = DataBindingUtil.inflate(inflater, R.layout.fragment_boutique, container, false);
        return binding.getRoot();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(Bou_ComicList_Item.class,new Pair<Integer, Integer>(R.layout.bou_item_comic, BR.comicListItem));
        map.put(Bou_ReturnData.class,new Pair<Integer, Integer>(R.layout.bou_galleryitem,BR.galleryAdapter));
        map.put(BouItemSecond.class,new Pair<Integer, Integer>(R.layout.bou_item_second,BR.second));
         ptr = (PullToRefresh) binding.getRoot().findViewById(R.id.bou_PullToRefresh);
        progressbar = ((ProgressBar) binding.getRoot().findViewById(R.id.bou_progressbar));
        recycle = ptr.getRefreshableView();
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
          bou_recyclerAdapter = new Bou_RecyclerAdapter(getContext(), new ArrayList<>(), map);
        binding.setVariable(BR.bouAdapter, bou_recyclerAdapter);
        recycle.setAdapter(bou_recyclerAdapter);

        NetUtils.getData(ClassType.BoutiqueBean, this);



        recycle.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
                c.drawColor(Color.GRAY);
            }

            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,1);
            }
        });
        ptr.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> pullToRefreshBase) {
//                ILoadingLayout loadingLayoutProxy = rec_ptr.getLoadingLayoutProxy(true, false);
//                loadingLayoutProxy.setPullLabel("有妖气！！！");
//                loadingLayoutProxy.setLoadingDrawable(getContext().getResources().getDrawable(R.mipmap.u17_refresh_eye_nictation2));
                ptr.setRefreshing(true);
                bou_recyclerAdapter.removeAll();
                NetUtils.getData(ClassType.BoutiqueBean,BoutiqueFragment.this);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> pullToRefreshBase) {
//                ILoadingLayout loadingLayoutProxy = rec_ptr.getLoadingLayoutProxy(false, true);
//                loadingLayoutProxy.setRefreshingLabel(null);
//                loadingLayoutProxy.setPullLabel(null);
//                loadingLayoutProxy.setLoadingDrawable(null);
//                if (isHasMore) {
//                    rec_ptr.setRefreshing(true);
//                    NetUtils.getData(ClassType.RecommendBean, RecommendFragment.this, ++pager);

                    ptr.onRefreshComplete();
//                    loadingLayoutProxy.setPullLabel("队长大人！！到底啦！！！");
//                    loadingLayoutProxy.setLoadingDrawable(getContext().getResources().getDrawable(R.mipmap.main_reminder_bottom));
                }
            }
        );


    }

    @Override
    public void onResponse(Call<BoutiqueBean> call, Response<BoutiqueBean> response) {


        Bou_ReturnData returnData = response.body().getData().getReturnData();

        bou_recyclerAdapter.add(0,returnData);
        bou_recyclerAdapter.add(1,new BouItemSecond());
        List<Bou_ComicList_Item> comicLists = returnData.getComicLists();

        for (int i = 0; i < comicLists.size(); i++) {
            if (comicLists.get(i).getComics().size()==0) {
                comicLists.remove(i);
            }
        }
        bou_recyclerAdapter.addAll(comicLists);

        ptr.onRefreshComplete();
        progressbar.setVisibility(View.GONE);

    }

    @Override
    public void onFailure(Call<BoutiqueBean> call, Throwable t) {

    }
}
